package game;

import game.console.Console;

import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


public abstract class Abstract2DMap extends AbstractRunnable
{
    protected short cellSize = 16;
    
    private Position size;
    
    public Position getSize()
    {
        return size;
    }
    
    public void setSize(Position size)
    {
        this.size = size;
    }
    
    public void recalcSize(Dimension windowSize)
    {
        Position maxXY = calcMaxSize(new Position(windowSize));
        setSize(maxXY);
    }
    
    public static int calculateIndex(Position position, Position size)
    {
        return calculateIndex(position.getX(), position.getY(), size);
    }
    
    public static int calculateIndex(int x, int y, Position size)
    {
        if (x < 0 || x > size.getWidth() || y < 0 || y > size.getHeight())
        {
            return 0;
        }
        int index = (size.getWidth() * y) + x;
        return index;
    }
    
    public static Position calculatePosition(int index, Position size)
    {
        int x = 0;
        int y = 0;
        // find y
        y = index / size.getX();
        x = index - (y * size.getX());
        return new Position(x, y);
    }
    
    public Position calcMaxSize(Position windowSize)
    {
        int width = windowSize.getWidth();
        if (width % cellSize != 0)
        {
            width -= width % cellSize;
        }
        width /= cellSize;
        int height = windowSize.getHeight();
        if (height % cellSize != 0)
        {
            height -= height % cellSize;
        }
        height /= cellSize;
        return new Position(width, height);
    }
    
    public Position calcCoord(java.awt.Point p)
    {
        int x = (int) (p.getX() / cellSize);
        int y = (int) (p.getY() / cellSize);
        return new Position(x, y);
    }
    
    @Override
    public KeyListener getKeyboardControl()
    {
        return new KeyAdapter() 
        {
            @Override
            public void keyPressed(KeyEvent e)
            {
                if (isRunning() == false || Console.getInstance().isOpen())
                {
                    e.consume();
                }
                else
                {
                    short newCellSize = cellSize;
                    switch (e.getKeyCode())
                    {
                        case KeyEvent.VK_1:
                        {
                            newCellSize = 1;
                            break;
                        }
                        case KeyEvent.VK_2:
                        {
                            newCellSize = 2;
                            break;
                        }
                        case KeyEvent.VK_3:
                        {
                            newCellSize = 3;
                            break;
                        }
                        case KeyEvent.VK_4:
                        {
                            newCellSize = 4;
                            break;
                        }
                        case KeyEvent.VK_5:
                        {
                            newCellSize = 5;
                            break;
                        }
                        case KeyEvent.VK_6:
                        {
                            newCellSize = 6;
                            break;
                        }
                        case KeyEvent.VK_7:
                        {
                            newCellSize = 7;
                            break;
                        }
                        case KeyEvent.VK_8:
                        {
                            newCellSize = 8;
                            break;
                        }
                        case KeyEvent.VK_9:
                        {
                            newCellSize = 9;
                            break;
                        }
                        case KeyEvent.VK_0:
                        {
                            newCellSize = 10;
                            break;
                        }
                    }
                    if (newCellSize > 0 && newCellSize != cellSize)
                    {
                        AbstractGame.log("cellSize: " + (cellSize = newCellSize));
                        requestRestart();
                    }
                }
            }
        };
    }
}
